public class CanvasTop extends Canvas {
  Node trunkWorld ;
  Polygon window;
  public CanvasTop(PVector origin, int h, int w, color backgroundColor, Node trunkWorld, Polygon window) {
    super(origin, h, w, backgroundColor);
    this.window = window;
    this.trunkWorld = trunkWorld;
  }

  @Override
    public void draw() {
    float w0 = PVector.sub(window.getPoint(0),window.getPoint(3)).mag();
    float h0 = PVector.sub(window.getPoint(0),window.getPoint(1)).mag();
    PVector up = window.getPoint(0);
    //Matrix4 scaling = Matrix4.scaling(w/abs(x0-x1),h/abs(y0-y1)).multiplyMatrix(Matrix4.rotation(-1.0f*window.getRotatedAngle()).multiplyMatrix(Matrix4.translation(up.x*-1.0f,up.y*-1.0f)));
    Matrix4 scaling = Matrix4.scaling(w/w0,h/h0).multiplyMatrix(Matrix4.rotation(-1.0f*window.getRotatedAngle()).multiplyMatrix(Matrix4.translation(up.x*-1.0f,up.y*-1.0f)));
    
    ArrayList<Polygon> windowView = new ArrayList<Polygon>();
    for (Polygon p : trunkWorld.getFigures()) {
      ArrayList<Polygon> cut = p.cut(window);
      windowView.addAll(cut);
    }
    
    pg.beginDraw();
    pg.background(backgroundColor);
    for (Polygon p : windowView) {
      p.transform(scaling);
      p.draw(pg);
    }
    pg.endDraw();
    image(pg, origin.x, origin.y);
  }
}

